import Vue from "vue/dist/vue.js";
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
	state:{
		count:0,
		age:24,
		todos:[
		   {id:1,text:'第一个',done:true},
		   {id:2,text:'第二个',done:false}
		]
	},
	getters:{
		doneTodos:state => {
			return state.todos.filter(todo => todo.done)
		},
		doneTodosCount:(state,getters) => {
			//Getter 也可以接受其他 getter 作为第二个参数：
			return getters.doneTodos.length
		},
		getTodoByIdES6:(state) => (id) => {
			return state.todos.find(todo => todo.id===id)
		},
		getTodoByIdES5:function(state){
			return function(id){
				return state.todos.find(function(todo){
					 return todo.id===id
				})
			}
		}
	},
	mutations:{
		increment(state,payload){
			//变更状态
			state.count += payload.amount
		}
	},
	actions:{
		emitIncrement(context){
			//使用action来提交mutation
			context.commit('increment')
		},
		incrementDeconstr({commit}){
			//使用对象解构赋值简化代码
			commit('increment')
		},
		incrementAsync ({ commit },payload) {
			//在 action 内部执行异步操作
		    setTimeout(() => {
		      commit('increment',payload)
		    }, 1000)
  		}
	}
})
export default store;
